/*
 *   Copyright [2008] [Anay Nayak]
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.googlecode.equalsnhashcode.finders;

import com.googlecode.equalsnhashcode.EqualsHashcodeProblemDescriptor;
import com.googlecode.equalsnhashcode.utils.Lists;
import com.googlecode.equalsnhashcode.utils.NonStaticFieldConstraint;
import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;

import java.util.Collections;
import java.util.List;

public class ProblemFinder {
    private PsiClass psiClass;
    private InspectionManager inspectionManager;
    private String message;

    public ProblemFinder(PsiClass psiClass, InspectionManager inspectionManager, String message) {
        this.psiClass = psiClass;
        this.inspectionManager = inspectionManager;
        this.message = message;
    }

    public List<ProblemDescriptor> process(PsiMethod psiMethod) {
        if (psiMethod == null) {
            return Collections.emptyList();
        }

        PsiFieldElementVisitor psiElementVisitor = new PsiFieldElementVisitor();
        psiMethod.acceptChildren(psiElementVisitor);


        List<PsiField> fieldsToConsider = Lists.create(psiClass.getFields());
        fieldsToConsider = Lists.filter(fieldsToConsider, new NonStaticFieldConstraint());
        fieldsToConsider.removeAll(psiElementVisitor.getPsiFields());
        return new EqualsHashcodeProblemDescriptor(inspectionManager, message).problemDescriptorsFor(fieldsToConsider);
    }

}
